//
//  CardGraph.m
//  cherryPicker
//
//  Created by Claire on 12. 1. 8..
//  Copyright (c) 2012년 Happy. All rights reserved.
//

#import "CardGraph.h"

@implementation CardGraph

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    CGContextRef c = UIGraphicsGetCurrentContext();
    
    [self drawBar:c usedValue:_usedValue limitPrice:_limitPrice];
}

- (void)drawBar:(CGContextRef) c
      usedValue:(float)usedValue
     limitPrice:(float)limitPrice{
    
    float barWidth = 0.0f;
    float barHeight = 15.0f;
    float limitLine = 0.0f;
    float beta = 7.0f;
    
    if (usedValue > limitPrice*1.1f) {
        barWidth = self.frame.size.width;
        limitLine = self.frame.size.width*limitPrice/usedValue;
    }else{
        barWidth = self.frame.size.width*usedValue/(limitPrice*1.1f);
        limitLine = self.frame.size.width*1.0f/1.1f;
    }
    
    CGContextSetRGBFillColor(c, 21.0f/255.0f, 117.0f/255.0f, 217.0f/255.0f, 1.0);
    CGContextFillRect(c,CGRectMake(0.0f, self.frame.size.height/2 - barHeight/2 + beta, barWidth, barHeight + beta));
    CGContextFillPath(c);
    
    CGContextSetRGBStrokeColor(c, 233.0f/255.0f, 53.0f/255.0f, 50.0f/255.0f, 1.0);
    CGContextMoveToPoint(c, limitLine, 0.0f);
    CGContextAddLineToPoint(c, limitLine, self.frame.size.height);
    CGContextStrokePath(c);
    
    UILabel* labLimitPrice = [[UILabel alloc]initWithFrame: CGRectMake(limitLine - 35.0f, 0.0f, 30.0f, 21.0f)];
    [labLimitPrice setFont:[UIFont systemFontOfSize:10]];
    [labLimitPrice setTextColor:[UIColor redColor]];
    [labLimitPrice setBackgroundColor:[UIColor clearColor]];
    [labLimitPrice setText:[NSString stringWithFormat:@"%.0f만원", limitPrice/10000.0f]];
    [labLimitPrice sizeToFit];
    
    [self addSubview:labLimitPrice];
    
}

- (void)updateData:(float)usedValue
limitPrice:(float)limitPrice{
    _usedValue = usedValue;
    _limitPrice = limitPrice;
    
    [self setNeedsDisplay];
}


@end
